﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Runtime.Extensions;


namespace PServiceBus.Redis {
    public class RedisMQ<TObject> : RedisCollection<TObject> where TObject : class {

        public override IEnumerator<TObject> GetObjects() {
            TObject @result = default(TObject);
            try {
                var client = GetConnection(READ_ONLY_KEY);
                var next = client.BLPop(Key, 0);
                var data = next != null && next.Length > 0 ? next[1] : null;
                @result = data.Deserialize<TObject>();
            } catch { }
            if (@result == null) yield break;
            yield return @result;
        }

        public override bool Add(IEnumerable<TObject> list) {
            return Query(client => {
                foreach (var obj in list) client.RPush(Key, obj.Serialize());
                return true;
            });
        }

        public override bool Add(TObject obj) {
            return Query(client =>
            {
                return client.RPush(Key, obj.Serialize()) == 1;
            });
        }

        public override bool Delete(IEnumerable<Func<TObject, bool>> conditions) {
            throw new NotImplementedException();
        }

        public override bool Delete(IEnumerable<TObject> list) {
            throw new NotImplementedException();
        }

        public override bool Delete(Func<TObject, bool> condition) {
            throw new NotImplementedException();
        }

        public override bool Delete(TObject obj) {
            throw new NotImplementedException();
        }

        public override bool Exists(Func<TObject, bool> condition) {
            throw new NotImplementedException();
        }

        public override bool Exists(TObject obj) {
            throw new NotImplementedException();
        }

        public override TObject Get(Func<TObject, bool> condition) {
            throw new NotImplementedException();
        }

        public override TObject Get(Guid id) {
            throw new NotImplementedException();
        }

        public override bool Refresh(TObject obj) {
            throw new NotImplementedException();
        }
    }
}
